﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using Properties;

namespace YJKPretreatmentModeling
{
	public partial class DlgMultAddLevels : System.Windows.Forms.Form
	{
		public DlgMultAddLevels()
		{
			this.InitializeComponent();
		}

		public DlgMultAddLevels(UIApplication app, List<LevelsDataSource> srcData, double srcEle, int addType, string strPrefix, string strSuffix, string strNewNumber, bool isChinese)
		{
			this.m_application = app;
			this.m_docment = app.ActiveUIDocument.Document;
			this.systemLevelsDatum = srcData;
			this.startEle = srcEle;
			this.type = addType;
			this.newLevelsDatum = new List<LevelsDataSource>();
			this.totalH = 0.0;
			this.InitializeComponent();
			this.InitLevelType();
			this.m_StrPrefix = strPrefix;
			this.m_StrSuffix = strSuffix;
			this.m_StrNewNumber = strNewNumber;
			this.m_IsChinese = isChinese;
		}

		public bool StringIsNumber(string s)
		{
			return false;
		}

		public void InitLevelType()
		{
			foreach (Element element in ElementFiltering.FindSpecialElements(this.m_docment, typeof(LevelType), BuiltInCategory.OST_Levels))
			{
				string name = (element as LevelType).Name;
				if ((name == "上标头" || name == "下标头" || name == "正负零标高") && !this.cmbSymType.Items.Contains(name))
				{
					this.cmbSymType.Items.Add(name);
				}
			}
			if (this.cmbSymType.Items.Count > 0)
			{
				this.cmbSymType.SelectedIndex = 0;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			int num = 0;
			if (this.tbStartNum.Text != "" && !int.TryParse(this.tbStartNum.Text, out num))
			{
				YJKMessageBox.Show("起始编号只能是整数！");
				this.tbStartNum.Focus();
				return;
			}
			if (!Assist.StringUtil.isNumberic2(this.tbDefaultH.Text))
			{
				YJKMessageBox.Show("楼层高度只能是正实数！");
				this.tbDefaultH.Focus();
				return;
			}
			if (!Assist.StringUtil.isNumberic1(this.tbSameNum.Text))
			{
				YJKMessageBox.Show("楼层数量只能为正整数！");
				this.tbSameNum.Focus();
				return;
			}
			if (this.tbSameNum.Text == "" || this.tbDefaultH.Text == "")
			{
				YJKMessageBox.Show("楼层数量、默认层高不能为空");
				return;
			}
			if (this.tbStartNum.Text == "")
			{
				if (Convert.ToInt32(this.tbSameNum.Text) != 1)
				{
					YJKMessageBox.Show("起始编号为空时只能有一个相同楼层！");
					return;
				}
				LevelsDataSource levelsDataSource = new LevelsDataSource();
				levelsDataSource.Name = this.tbBefore.Text + this.tbAfter.Text;
				if (this.type == 0)
				{
					levelsDataSource.Elevation = this.startEle + Convert.ToDouble(this.tbDefaultH.Text);
				}
				else
				{
					levelsDataSource.Elevation = this.startEle - Convert.ToDouble(this.tbDefaultH.Text);
				}
				levelsDataSource.LevelHeight = Convert.ToDouble(this.tbDefaultH.Text);
				levelsDataSource.Type = this.cmbSymType.SelectedItem.ToString();
				this.newLevelsDatum.Add(levelsDataSource);
			}
			else
			{
				bool flag = false;
				if (this.type == 0)
				{
					flag = (Convert.ToInt32(this.tbStartNum.Text) < 0 && Convert.ToInt32(this.tbSameNum.Text) + Convert.ToInt32(this.tbStartNum.Text) > 0);
				}
				else if (this.type == 1)
				{
					flag = (Convert.ToInt32(this.tbStartNum.Text) > 0 && Convert.ToInt32(this.tbSameNum.Text) - Convert.ToInt32(this.tbStartNum.Text) > 0);
				}
				int num2 = Convert.ToInt32(this.tbSameNum.Text);
				int num3 = (this.type == 0) ? (Convert.ToInt32(this.tbStartNum.Text) + num2 - 1) : Convert.ToInt32(this.tbStartNum.Text);
				if (flag && this.type == 0)
				{
					num3++;
				}
				if (this.type == 0)
				{
					this.startEle += Convert.ToDouble(this.tbDefaultH.Text) * (double)num2;
				}
				else
				{
					this.startEle -= Convert.ToDouble(this.tbDefaultH.Text);
				}
				for (int i = 0; i < num2; i++)
				{
					LevelsDataSource levelsDataSource2 = new LevelsDataSource();
					int num4;
					if (flag)
					{
						num4 = num3 - i;
						if (num4 <= 0)
						{
							num4--;
						}
					}
					else
					{
						num4 = num3 - i;
					}
					string text = num4.ToString();
					if (this.cbIndexUseCN.Checked)
					{
						text = ArabicNumeralsConvertChineseDigitalKit.NumToChn(text);
					}
					levelsDataSource2.Name = this.tbBefore.Text + this.ChangeUndergroundLevelName(text) + this.tbAfter.Text;
					levelsDataSource2.Elevation = this.startEle - Convert.ToDouble(this.tbDefaultH.Text) * (double)i;
					levelsDataSource2.LevelHeight = Convert.ToDouble(this.tbDefaultH.Text);
					levelsDataSource2.Type = this.cmbSymType.SelectedItem.ToString();
					this.newLevelsDatum.Add(levelsDataSource2);
				}
			}
			this.totalH = Convert.ToDouble(this.tbDefaultH.Text) * Convert.ToDouble(this.tbSameNum.Text);
			base.DialogResult = DialogResult.OK;
		}

		private void DlgMultAddLevels_Load(object sender, EventArgs e)
		{
            this.tbBefore.Text = Properties.Settings.Default.NLBefore;
			if (this.m_StrPrefix.CompareTo("") != 0)
			{
				this.tbBefore.Text = this.m_StrPrefix;
			}
            this.tbStartNum.Text = Properties.Settings.Default.NLStartNum.ToString();
			if (this.m_StrNewNumber.CompareTo("") != 0)
			{
				this.tbStartNum.Text = this.m_StrNewNumber;
			}
            this.tbAfter.Text = Properties.Settings.Default.NLAfter;
			if (this.m_StrSuffix.CompareTo("") != 0)
			{
				this.tbAfter.Text = this.m_StrSuffix;
			}
            this.tbSameNum.Text = Properties.Settings.Default.NLSameNum.ToString();
            this.tbDefaultH.Text = Properties.Settings.Default.NLHeight.ToString();
            this.cmbSymType.SelectedIndex = Properties.Settings.Default.NLSymType;
			if (this.m_IsChinese)
			{
				this.cbIndexUseCN.Checked = true;
			}
			this.UpdatePreviewItem();
		}

		private void tbStartNum_Leave(object sender, EventArgs e)
		{
		}

		private void tbDefaultH_Leave(object sender, EventArgs e)
		{
		}

		private void tbSameNum_Leave(object sender, EventArgs e)
		{
		}

		private void tbBefore_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void tbStartNum_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void tbAfter_TextChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void cbIndexUseCN_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdatePreviewItem();
		}

		private void tbBefore_Enter(object sender, EventArgs e)
		{
			this.tbBefore.SelectAll();
		}

		private void tbStartNum_Enter(object sender, EventArgs e)
		{
			this.tbStartNum.SelectAll();
		}

		private void tbAfter_Enter(object sender, EventArgs e)
		{
			this.tbAfter.SelectAll();
		}

		private void tbDefaultH_Enter(object sender, EventArgs e)
		{
			this.tbDefaultH.SelectAll();
		}

		private void tbSameNum_Enter(object sender, EventArgs e)
		{
			this.tbSameNum.SelectAll();
		}

		private void tbBefore_Click(object sender, EventArgs e)
		{
			this.tbBefore.SelectAll();
		}

		private void tbStartNum_Click(object sender, EventArgs e)
		{
			this.tbStartNum.SelectAll();
		}

		private void tbAfter_Click(object sender, EventArgs e)
		{
			this.tbAfter.SelectAll();
		}

		private void tbDefaultH_Click(object sender, EventArgs e)
		{
			this.tbDefaultH.SelectAll();
		}

		private void tbSameNum_Click(object sender, EventArgs e)
		{
			this.tbSameNum.SelectAll();
		}

		private void UpdatePreviewItem()
		{
			try
			{
				string text = this.tbStartNum.Text;
				if (this.cbIndexUseCN.Checked)
				{
					text = ArabicNumeralsConvertChineseDigitalKit.NumToChn(this.tbStartNum.Text);
				}
				text = this.ChangeUndergroundLevelName(text);
				this.lblPreview.Text = "层名预览：" + this.tbBefore.Text + text + this.tbAfter.Text;
			}
			catch
			{
				this.lblPreview.Text = "层名预览：" + this.tbBefore.Text + "零" + this.tbAfter.Text;
			}
		}

		private string ChangeUndergroundLevelName(string oldName)
		{
			return oldName.Replace("负", "地下").Replace("-", "B");
		}

		private UIApplication m_application;

		private Document m_docment;

		private List<LevelsDataSource> systemLevelsDatum;

		private double startEle;

		private int type;

		public List<LevelsDataSource> newLevelsDatum;

		public double totalH;

		private string m_StrPrefix = "";

		private string m_StrSuffix = "";

		private string m_StrNewNumber = "";

		private bool m_IsChinese;
	}
}
